# coding=utf-8
import json
import os
import requests as http
import time

WORK_PATH = os.getcwd()
# 爬取页数，一页有20条数据
GET_COUNT = 1000
GTE_SLEEP = 1
http.DEFAULT_RETRIES = 5  # 增加重试连接次数
s = http.session()
s.keep_alive = False  # 关闭多余连接


def get_filename(index, suffix):
    time_tuple = time.localtime(time.time())
    t = ''
    for i in range(5):
        # print(time_tuple[i])
        t = t + str(time_tuple[i]) + '_'
    return t + str(index) + '.' + suffix


def trans_format(time_string, from_format, to_format='%Y.%m.%d %H:%M:%S'):
    """
    @note 时间格式转化
    :param time_string:
    :param from_format:
    :param to_format:
    :return:
    """
    time_struct = time.strptime(time_string, from_format)
    times = time.strftime(to_format, time_struct)
    return times


class Reptile:
    def __init__(self):
        print('Reptile init')
        # create paths
        if not os.path.exists('./video'):
            os.makedirs('./video')
        if not os.path.exists('./content'):
            os.makedirs('./content')
        if not os.path.exists('cookie.txt'):
            open('cookie.txt', 'w')
        self.cookie = open('cookie.txt', 'r')
        self.header = {
            "Accept": "application/json, text/plain, */*",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15",
            "Cookie": self.cookie
        }

    def __download_video(self, video_link, save_path) -> int:

        pass

    def __download_content(self, content_link, save_path) -> int:
        pass

    def __get_video_list_json(self) -> dict:
        pass

    def __run(self) -> str:
        # 获取当前页json数据
        video_json: dict = self.__get_video_list_json()

        pass

    def start_reptile(self, count) -> int:
        """
        :return: 返回爬取数据条目数
        """
        index: int = 0
        page_code = '0'
        for index in range(count):
            page_code: str = self.__run(page_code)
        return index


if __name__ == '__main__':
    r = Reptile()
    r.start_reptile(2)
